package org.example.tlias.mapper;

import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.example.tlias.pojo.Emp;
import org.example.tlias.pojo.EmpQueryParam;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Mapper
public interface EmpMapper {
    @Select("select count(*) from emp e, dept d where e.dept_id = d.id")
    Long count();

    @Select("select e.id, e.username, e.password, e.name, e.gender, e.phone, e.job, e.salary, e.image, e.entry_date, e.dept_id, e.create_time, e.update_time, d.name dept_ame from emp e,dept d " +
            "where e.dept_id = d.id order by e.entry_date desc limit #{page},#{pageSize}")
    List<Emp> page(Integer page, Integer pageSize);

    @Select("select e.id, e.username, e.password, e.name, e.gender, e.phone, e.job, e.salary, e.image, e.entry_date, e.dept_id, e.create_time, e.update_time, d.name dept_ame from emp e,dept d where e.dept_id = d.id order by e.entry_date desc ")
    List<Emp> page1();

    List<Emp> page2(String name, Integer gender, LocalDate begin, LocalDate end);

    List<Emp> page3(EmpQueryParam empQueryParam);

    @Options(useGeneratedKeys = true, keyProperty = "id")// 插入数据后，返回主键值, keyProperty指定主键属性名 useGeneratedKeys指定是否使用自动生成的主键
    void save(Emp emp);

    void deleteByIds(List<Integer> ids);

    Emp getById(Integer id);

    void update(Emp emp);

   @Select("select (case\n" +
           "    when job = 1 then '班主任'\n" +
           "    when job = 2 then '讲师'\n" +
           "    when job = 3 then '学工主管'\n" +
           "    when job = 5 then '咨询师'\n" +
           "    else '其它'\n" +
           "    end) ops,\n" +
           "       count(*) num\n" +
           "\n" +
           "from emp group by job order by num")
//   @MapKey("ops")
    List<Map<String,Object>> countEmpJobData();
    @MapKey("name")
    List<Map> countEmpGenderData();

    @Select("select id,id, username, password, name, gender, phone, job, salary, image, entry_date, dept_id, create_time, update_time from emp")
    List<Emp> list();


    @Select("select id, username, name from emp where username = #{username} and password = #{password}")
    Emp selectByUsernameAndPassword(Emp emp);
}
